﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.XepLoaiPhoThongService;
namespace eTMS.Controllers{
public class XepLoaiPhoThongControllerBase : CommonController{
	public XepLoaiPhoThongControllerBase()
		: base()
	{	
		this.dao = new XepLoaiPhoThongServiceClient();
	}
	~XepLoaiPhoThongControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private XepLoaiPhoThongEntity oXepLoaiPhoThong;	
	public XepLoaiPhoThongEntity XepLoaiPhoThongObject{
		get{
			return oXepLoaiPhoThong;
		}
		set{
			oXepLoaiPhoThong=value;
		}
	}
	protected XepLoaiPhoThongServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSXepLoaiPhoThong(false);			
			return _Table;
		}
	}
	public void LoadAllDSXepLoaiPhoThong(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oXepLoaiPhoThong==null)return false;
		bool result=true;
		try{
			oXepLoaiPhoThong = dao.SaveRet(oXepLoaiPhoThong);
		}
		catch{
			result=false;
		}
		if(result){
			_ListXepLoaiPhoThong.Add(oXepLoaiPhoThong);
		}
		return result;
	}
	public bool Update(){
		if(oXepLoaiPhoThong==null)return false;
		return dao.Update(oXepLoaiPhoThong);
	}
	public bool Delete(){
		if(oXepLoaiPhoThong==null)return false;
		bool retvalue = dao.Delete(oXepLoaiPhoThong);
		if(retvalue){			
			_ListXepLoaiPhoThong.Remove(oXepLoaiPhoThong);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 XepLoaiPT_ID){
		bool retvalue = dao.DeleteByID(XepLoaiPT_ID);		
		if(retvalue){
			XepLoaiPhoThongEntity oDeleted=FindXepLoaiPhoThong(XepLoaiPT_ID);
			if(oDeleted!=null){
				_ListXepLoaiPhoThong.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oXepLoaiPhoThong = new XepLoaiPhoThongEntity();
	}
	//Check if exists MaXepLoaiPT
	public bool CheckExistsMaXepLoaiPT(object value){	
		return dao.SelectOneByMaXepLoaiPT(value.ToString()).MaXepLoaiPT!=null;
    }    
    //Copy data from data row in grid to object
    public XepLoaiPhoThongEntity MapDataRowToObject(DataRow row){
		XepLoaiPhoThongEntity o = new XepLoaiPhoThongEntity();
		 o.MaXepLoaiPT=Convert.ToString(row["MaXepLoaiPT"]);
		 o.TenXepLoaiPT=Convert.ToString(row["TenXepLoaiPT"]);
		 o.MoTaXepLoaiPT=Convert.ToString(row["MoTaXepLoaiPT"]);
		 if(!row.IsNull("XepLoaiPT_ID"))
		    o.XepLoaiPT_ID=(Int32)(row["XepLoaiPT_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(XepLoaiPhoThongEntity o,DataRow row){
		row["MaXepLoaiPT"]=o.MaXepLoaiPT;
		row["TenXepLoaiPT"]=o.TenXepLoaiPT;
		row["MoTaXepLoaiPT"]=o.MoTaXepLoaiPT;
		row["XepLoaiPT_ID"]=o.XepLoaiPT_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.XepLoaiPhoThongObject,row);
    }
	public void LoadObject(Int32 XepLoaiPT_ID){
		XepLoaiPhoThongEntity oLoad=FindXepLoaiPhoThong(XepLoaiPT_ID);
		if(oLoad==null){
			oXepLoaiPhoThong=this.LoadXepLoaiPhoThong(XepLoaiPT_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oXepLoaiPhoThong.IsNullXepLoaiPT_ID;
			if(!bNotIsNull){
				this.ListXepLoaiPhoThong.Add(oXepLoaiPhoThong);
			}
		}
		else{
			oXepLoaiPhoThong=oLoad;
		}
	}
	#region CachedList XepLoaiPhoThong
	private static List<XepLoaiPhoThongEntity> _ListXepLoaiPhoThong=new List<XepLoaiPhoThongEntity>();
    public List<XepLoaiPhoThongEntity>ListXepLoaiPhoThong{
		get{
			return _ListXepLoaiPhoThong;
		}
	}
	public static List<XepLoaiPhoThongEntity>ListXepLoaiPhoThongStatic{
		get{
			return _ListXepLoaiPhoThong;
		}
	}
	public XepLoaiPhoThongEntity FindXepLoaiPhoThong(Int32 XepLoaiPT_ID){
		XepLoaiPhoThongEntity oResult=this.ListXepLoaiPhoThong.Find(delegate(XepLoaiPhoThongEntity oFind){
			bool result=true; 
			result&=oFind.XepLoaiPT_ID.Equals(XepLoaiPT_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllXepLoaiPhoThong(bool clearCache){
		if(_ListXepLoaiPhoThong.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		XepLoaiPhoThongServiceClient dao = new XepLoaiPhoThongServiceClient();
		_ListXepLoaiPhoThong=new List<XepLoaiPhoThongEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public XepLoaiPhoThongEntity LoadXepLoaiPhoThong(Int32 XepLoaiPT_ID){			
		XepLoaiPhoThongEntity o=dao.SelectOne(XepLoaiPT_ID);		
		return o;
	}
	#endregion
}
}
